export const getJsonObjParams = (parameters, apiDefinations) => {
  var values = [];
  var keyIndex = 1;

  for (var index in parameters) {
    var parameter = parameters[index];
    var type = parameter.type;
    var inx = parameter.in;
    var schema = parameter.schema;
    var required = parameter.reqyes;
    if (required == undefined) {
      required = "false";
    }
    if ("body" == inx) {
      required = "true";
    }
    var obj = {};
    if (schema != undefined && schema.$ref != undefined) {
      var obj = getSchemaRefParams(apiDefinations, schema, keyIndex);

      if (obj != undefined && obj != null) {
        
        obj.ptype = inx;
        obj.reqyes = required;
        values[index] = obj;
        keyIndex = keyIndex + 1;
      }
    } else {
      var name = parameter.name;
      var inx = parameter.in;
      var type = parameter.type;
      var description = parameter.description;
      obj.key = keyIndex;
      obj.name = name;
      obj.ptype = inx;
      obj.type = type;
      obj.formatter = parameter.format;
      var collectionFormat = parameter.collectionFormat;
      if (collectionFormat != undefined && collectionFormat == "multi") {
        var items = parameter.items;
        if (items != undefined && items.type != undefined && items.type == "file") {
          obj.type = 'file[]';
        }
      }
      obj.description = description;
      obj.reqyes = required;
      values[index] = obj;
      keyIndex = keyIndex + 1;
    }
  }
  if (values.length > 0) {
    values.sort(compare);
  }

  return values;
}
// debugger
const compare = function (obj1, obj2) {
  var p1 = obj1.ptype;
  var p2 = obj2.ptype;
  var n1 = 0;
  var n2 = 0;
  if ('header' == p1) {
    n1 = 1;
  }
  if ('path' == p1) {
    n1 = 2;
  }
  if ('query' == p1) {
    n1 = 3;
  }
  if ('body' == p1) {
    n1 = 4;
  }


  if ('header' == p2) {
    n2 = 1;
  }
  if ('path' == p2) {
    n2 = 2;
  }
  if ('query' == p2) {
    n2 = 3;
  }
  if ('body' == p2) {
    n2 = 4;
  }
  return n1 - n2;
}
export const renderKey = function (key) {
  if ("com.alibaba.fastjson.JSONObject" == key) {
    key = "JSONObject";
  } else {
    var fxStartIndex = key.indexOf("<");
    var fxEndIndex = key.indexOf(">");
    if (fxStartIndex > 0 && fxEndIndex > 0) {
      key = key.replace("<", "«");
      key = key.replace(">", "»");
    }
  }
  return key;
}
export const getSchemaRefParams = (apiDefinations, schema, keyIndex) => {
  if (schema != undefined && schema.$ref != undefined) {
    var temp = schema.$ref.split("/");
    var key = temp[temp.length - 1];
    key = renderKey(key);
    var schemaRefObj = apiDefinations[key];
    var obj = {};
    if(schemaRefObj==undefined){
      if (key.indexOf("«Object»") >-1) {
        key = key.replace("«Object»","");
      }
      
      obj.type = 'object';
      obj.key = keyIndex;
      obj.name = key;
      obj.description = "未识别的自定义对象,可以尝试以body体调试模式";
      obj.children=[];
      return obj;
    }
    
    var schemaRefType = schemaRefObj.type;
    

    if (schemaRefType && (schemaRefType == "Object" || schemaRefType == "object")) {
      var title = schemaRefObj.title;
      if (title != undefined) {
       
        obj.name = title;
      }
      obj.type = schemaRefType;
      obj.key = keyIndex;
      obj.reqyes = '';
      obj.children = getListObjParams(schemaRefObj, apiDefinations, keyIndex);
    }
    return obj;
  }
  return null;
}
export const getListObjParams = (schemaRefObj, apiDefinations, keyIndex) => {
  var values = [];
  var type = schemaRefObj.type;
  var index = 0;

  if (type == "object") {
    var properties = schemaRefObj.properties;
    var keyIndexChild = 0;

    for (var pname in properties) {
      keyIndexChild = keyIndexChild + 1;
      var keyIndexNew = keyIndex + "" + keyIndexChild;
      var obj = {};
      var property = properties[pname];
      var type = property.type;
      var description = property.description;
      if ("array" == type) {
        obj.name = 'array';
        obj.type = type;
        obj.key = keyIndexNew;
        obj.reqyes = '';
        obj.description = description;
        var items = property.items;
        if (items != undefined) {
          if (items.$ref != undefined) {
            var childrenTemplate = getSchemaRefParams(apiDefinations, items, keyIndexNew);
            if (childrenTemplate != undefined && childrenTemplate != null) {
              childrenTemplate.key = keyIndexNew;
              childrenTemplate.reqyes = '';
              childrenTemplate.type = "List<" + childrenTemplate.name + ">";
              childrenTemplate.description = description;
              childrenTemplate.name = pname;
              // values[index] = obj;
              values[index] = childrenTemplate;
              index = index + 1;
            }
          } else if (items.type != undefined) {
            var childrenTemplate = {};
            childrenTemplate.key = keyIndexNew;
            childrenTemplate.reqyes = '';
            childrenTemplate.type = "List<" + items.type + ">";
            childrenTemplate.description = description;
            childrenTemplate.name = pname;
            // obj.children = childrenTemplate;
            // values[index] = obj;
            values[index] = childrenTemplate;
            index = index + 1;
          }
        }
      } else {
        obj.key = keyIndexNew;
        obj.name = pname;
        obj.type = type;
        obj.formatter = property.format;
        obj.description = description;
        if (type == undefined && property.$ref != undefined) {
          var childrenTemplate = getSchemaRefParams(apiDefinations, property, keyIndexNew);
          if (childrenTemplate != undefined && childrenTemplate != null) {
            childrenTemplate.key = keyIndexNew;
            childrenTemplate.reqyes = '';
            childrenTemplate.description = description;

            // childrenTemplate.type = childrenTemplate.name;
            childrenTemplate.name = pname;//重写了泛型作为查询条件时，可能入参不对的情况
            values[index] = childrenTemplate;
            index = index + 1;
          }
        } else if (type != "object") {
          values[index] = obj;
          index = index + 1;
        } else if (type == "object") {//可能有bug需要特殊处理？
          values[index] = obj;
          index = index + 1;
        }
      }
    }
  }
  return values;
}

export const getResParam = (schemaRefObj, apiDefinations) => {
  var values = [];
  if (schemaRefObj == undefined) {
    return [];
  }
  var ref = schemaRefObj.$ref;
  var key = 1;
  if (ref != undefined) {
    var obj = getSchemaRefParams(apiDefinations, schemaRefObj, 1);
    obj.type = obj.name;
    obj.key = key;
    obj.name = "根节点";
    obj.description = "返回值根节点,后端接口返回结果数据类型描述";
    values[0] = obj;
  } else {
    var type = schemaRefObj.type;
    var items = schemaRefObj.items;
    if ((type != undefined && type == "array") && items != undefined) {
      if ("object" == items.type) {
        var obj = {};
        obj.type = "array<object>";
        obj.key = key;
        obj.name = "根节点";
        obj.description = "返回值根节点,后端接口返回结果数据类型描述";
        values[0] = obj;
      } else {
        var childrenTemplate = getSchemaRefParams(apiDefinations, items, 1);
        if (childrenTemplate != undefined && childrenTemplate != null) {
          childrenTemplate.key = 1;
          childrenTemplate.type = "List<" + childrenTemplate.name + ">";
          childrenTemplate.name = "根节点";
          childrenTemplate.description = "返回值根节点,后端接口返回结果数据类型描述";
          values[0] = childrenTemplate;
        }
      }
    } else {
      if (type != undefined && type == "object") {
        var obj = {};
        obj.key = key;
        obj.type = "object";
        obj.name = "根节点";
        obj.description = "返回值根节点,后端接口返回结果数据类型描述";
        values[0] = obj;
      } else {
        var obj = {};
        obj.type = type;
        obj.key = key;
        obj.name = "根节点";
        obj.description = "返回值根节点,后端接口返回结果数据类型描述";
        values[0] = obj;
      }
    }
  }
  return values;
}